<template>
  <div class="app-container">
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item label="">
        <el-input
          v-model="pageDto.where.title"
          placeholder="题库名"
          clearable
          @clear="search()"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="search()">查询</el-button>
        <el-button type="success" @click="handAdd()">添加题库</el-button>
        <el-button type="danger" @click="batchDelete()">批量删除</el-button>
      </el-form-item>
    </el-form>

    <!-- 表格 -->
    <el-table
      :data="repoList"
      style="width: 100%"
      @select="selectItem"
      @select-all="selectAll"
    >
      <el-table-column type="selection" />
      <el-table-column prop="title" label="题库名" />
      <el-table-column prop="radioCount" label="单选题" />
      <el-table-column prop="multiCount" label="多选题" />
      <el-table-column prop="judgeCount" label="判断题" />
      <el-table-column prop="remark" label="描述" />
      <el-table-column prop="createTime" label="创建时间" />
      <el-table-column label="操作" width="200">
        <template slot-scope="scope">
          <div>
            <el-button
              size="mini"
              type="success"
              icon="el-icon-edit"
              @click="handleEdit(scope.row)"
              >修改</el-button
            >
            <el-button
              size="mini"
              type="danger"
              icon="el-icon-delete"
              @click="handleDelete(scope.row.id)"
              >删除</el-button
            >
          </div>
        </template>
      </el-table-column>
    </el-table>
    <br />
    <el-pagination
      style="float: right"
      :current-page="pageDto.now"
      :page-sizes="[5, 10, 50, 100]"
      :page-size="pageDto.size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <!-- 添加/修改弹出框 -->
    <el-dialog title="题库管理" :visible.sync="dialogFormVisible">
      <el-form ref="ruleForm" :model="repo" :rules="rules" :v-model="repo">
        <el-form-item label="题库名称" prop="title">
          <el-input v-model="repo.title" autocomplete="off" />
        </el-form-item>
        <el-form-item label="题库备注" prop="remark">
          <el-input v-model="repo.remark" autocomplete="off" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addRef('ruleForm')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { page, create, update, deleteRepo, deleteRepoBatch } from '@/api/repo'
export default {
  data() {
    return {
      total: 0,
      repoList: [],
      pageDto: {
        now: 1,
        size: 5,
        where: {}
      },
      repo: {},
      dialogFormVisible: false,
      rules: {
        title: [{ required: true, message: '请输入题库名称', trigger: 'blur' }],
        remark: [{ required: true, message: '请输入题库备注', trigger: 'blur' }]
      },
      selectRepos: []
    }
  },
  created() {
    this.search()
  },
  methods: {
    search() {
      page(this.pageDto).then((res) => {
        this.repoList = res.list
        this.total = res.total
        this.pageDto.now = 1
      })
    },
    handleSizeChange(size) {
      this.pageDto.size = size
      this.search()
    },
    handleCurrentChange(page) {
      this.pageDto.now = page
      this.search()
    },
    handAdd() {
      this.repo = {}
      this.dialogFormVisible = true
    },
    handleEdit(row) {
      this.dialogFormVisible = true
      this.repo = row
    },
    handleDelete(id) {
      this.$confirm('此操作将永久删除该题库，是否继续？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteRepo(id).then((res) => {
          this.successCallback(res)
        })
      })
    },
    selectItem(selection) {
      this.selectRepos = selection
    },
    selectAll(selection) {
      this.selectRepos = selection
    },
    batchDelete() {
      this.$confirm('此操作将永久删除这些题库，是否继续？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteRepoBatch(this.selectRepos).then((res) => {
          this.successCallback(res)
        })
      })
    },
    successCallback(res) {
      this.dialogFormVisible = false
      if (res.code == 0) {
        this.pageDto.now = 1
        this.search()
      }
      this.$message({
        message: res.msg,
        type: res.code == 0 ? 'success' : 'error'
      })
    },
    addRef(ruleForm) {
      this.$refs[ruleForm].validate((vaild) => {
        if (vaild) {
          if (this.repo.id) {
            update(this.repo).then((res) => {
              this.successCallback(res)
            })
          } else {
            create(this.repo).then((res) => {
              this.successCallback(res)
            })
          }
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style>
</style>
